/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-03 14:57:46
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:17:28
 */

'use strict';
define([
    '../framework/base/baseView',
    '../framework/bean/productSet',
    './sets/createSets/createSets',
    '../../modules/business/feed/feed'
], function(
    BaseView,
    productSetBean,
    CreateSetsView,
    feed
) {
    function me() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function() {
            this.feedId = window.localStorage.getItem('weFeedId');
            if (this.feedId) {
                this.render();
            } else {
                showTip({
                    time: 5,
                    through: false,
                    type: "danger",
                    content: "feed id 获取失败!",
                    icon: "<i class='glyphicon glyphicon-flag'></i>",
                    close: true
                });
            }
        }

        this.render = function() {
            feed._init_tabs(3);
            self.$table = $('table[list-manager="sets"]');
            self.$table.listManager('init', {
                setTopVal: 228, //调整吸顶的高度,需要减去这个高度
                scrollDOM: $('.fmp-article'), //监听滚动条
                supportAjaxPage: true,
                pageCallback: function(query) {
                    self.query = $.extend(self._query, query);
                    // self.getList();
                }
            }, function(query) {
                self.query = query;
                self.getList();
            });
            self.event();
        }

        this.event = function() {
            $('#createSets').off('click').on('click', function() {
                new CreateSetsView().init(function(productSetId) {
                    self.getList();
                });
            })
            self.$table.off('click', 'td a.preview').on('click', 'td a.preview', function() {
                window.top.location.hash = $(this).data('href');
            })
            self.$table.off('click', 'td a.download').on('click', 'td a.download', function() {
                var url = $(this).data('url');
                if(url==null || url.length==0){
                	return false;
                }
                window.top.open(url);
            })
        }



        this.getList = function(disabledShowloading) {
            self.query.feed_id = self.feedId;
            if (disabledShowloading) {
                self.query._disabledShowloading = disabledShowloading;
            }
            productSetBean.query(self.query, function(result) {
                if ($('#sets-grid').length == 0) {
                    return;
                }
                $('#sets-grid').empty();
                var t = $('#t-sets-grid-tr').text();
                if (result.resultList && result.resultList.length > 0) {
                    for (var i = 0; i < result.resultList.length; i++) {
                        var res = result.resultList[i];
                        res.dataSize = res.data ? res.data.length : '-';
                        res.createdTime = self.kit.formatTime(res.created_time);
                        res.report_data_url = res.report_data_url||'';
                        res.report_data_status = res.report_data_status||'finished';//没有report_data_status字段，默认为完成
                        var tpl = _.template(t)(res);
                        $('#sets-grid').append(tpl);
                        var $downBtn = $('#sets-grid').find('tr[data-id="' + res.id + '"]').find('a.download');
                        if (res.report_data_status == 'finished') {
                        	$downBtn.removeClass('hidden');
                        }
                        if (res.report_data_status == 'running'||res.report_data_status == 'create_report') {
                        	$downBtn.removeClass('hidden').addClass('disabled').text('running');
                        }
                        if (res.report_data_status == 'fail') {
                        	$downBtn.removeClass('hidden').addClass('disabled').text('fail');
                        }
                    }
                    if (result.tSize && result.pageSize) {
                        result.tPage = self.culTotalPageNum(result.tSize, result.pageSize);
                        self.$table.listManager([{ 'resetTd': false, 'resetPageData': result }]);
                    }
                    self.updateStatus();
                }
            });
        }

        /**
         * { 轮询更新状态,如果全部是finish,则停止 }
         *
         * @method     updateStatus
         */
        this.updateStatus = function() {
            if (self.$table.find('tr td[data-report-status]').length == self.$table.find('tr td[data-report-status=finished],tr td[data-report-status=fail]').length) {
                console.info("所有数据刷新完成!")
            } else {
                setTimeout(function() {
                    console.info("---------------开始数据刷新完成!")
                    var disabledShowloading = true; //不显示loading
                    self.getList(disabledShowloading);
                }, 2000)
            }
        }

    }
    return new me();
});
